package com.gks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gks.entities.User;
import com.gks.entities.vo.UserVo;
import com.gks.mapper.UserMapper;
import com.gks.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        try {
            System.out.println("Starting registration process for user: " + user);
            
            // 检查账号是否已存在
            System.out.println("Checking if account exists: " + user.getAccount());
            User existingUser = userMapper.findByAccount(user.getAccount());
            System.out.println("Existing user check result: " + existingUser);
            
            if (existingUser != null) {
                System.out.println("Account already exists: " + user.getAccount());
                throw new RuntimeException("账号已存在");
            }
            
            System.out.println("Account is available, proceeding with registration");
            // 插入新用户
            int result = userMapper.insert(user);
            System.out.println("Insert result: " + result);
            
            if (result <= 0) {
                System.out.println("Failed to insert user");
                throw new RuntimeException("注册失败");
            }
            
            System.out.println("Registration successful");
        } catch (Exception e) {
            System.out.println("Registration failed with error: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("注册失败：" + e.getMessage());
        }
    }

    @Override
    public User login(User user) {
        try {
            // 根据账号查询用户
            User dbUser = userMapper.findByAccount(user.getAccount());
            if (dbUser == null) {
                throw new RuntimeException("账号不存在");
            }
            
            // 验证密码
            if (!dbUser.getPassword().equals(user.getPassword())) {
                throw new RuntimeException("密码错误");
            }
            
            return dbUser;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("登录失败：" + e.getMessage());
        }
    }

    @Override
    public UserVo selectUserByAccount(String account) {
        try {
            User user = userMapper.findByAccount(account);
            if (user == null) {
                return null;
            }
            
            UserVo vo = new UserVo();
            BeanUtils.copyProperties(user, vo);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询用户失败：" + e.getMessage());
        }
    }

    @Override
    public UserVo selectUserByNameLike(String namePattern) {
        try {
            // 使用模糊查询查找用户
            User user = userMapper.selectOne(
                new LambdaQueryWrapper<User>()
                    .like(User::getName, namePattern)
            );
            
            if (user == null) {
                return null;
            }
            
            // 转换为 UserVo
            UserVo userVo = new UserVo();
            userVo.setId(user.getId());
            userVo.setName(user.getName());
            userVo.setAccount(user.getAccount());
            
            return userVo;
        } catch (Exception e) {
            throw new RuntimeException("模糊查询用户失败：" + e.getMessage());
        }
    }

    @Override
    public void updateUserByAccount(User user) {
        try {
            // 检查用户是否存在
            User existingUser = userMapper.selectOne(
                new LambdaQueryWrapper<User>()
                    .eq(User::getAccount, user.getAccount())
            );
            
            if (existingUser == null) {
                throw new RuntimeException("用户不存在");
            }
            
            // 更新用户信息
            userMapper.update(user, 
                new LambdaQueryWrapper<User>()
                    .eq(User::getAccount, user.getAccount())
            );
        } catch (Exception e) {
            throw new RuntimeException("更新用户信息失败：" + e.getMessage());
        }
    }

    @Override
    public void changePassword(String account, String oldPassword, String newPassword) {
        // 验证旧密码
        User user = userMapper.findByAccount(account);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        if (!user.getPassword().equals(oldPassword)) {
            throw new RuntimeException("旧密码错误");
        }
        
        // 更新密码
        user.setPassword(newPassword);
        int result = userMapper.updateById(user);
        if (result <= 0) {
            throw new RuntimeException("修改密码失败");
        }
    }
}
